<?php



require "database.php";


require "../config/config.php";

$base=new Db($__conf__);

$post=$_POST;



$orderon=date("YmdHis").mt_rand(100000,999999);


$user=$base->doSql('select * from tp_user where token="'.$post["uid"].'"');
if (!$user) {
	return json([
		"code"=>400,
		"msg"=>"System Error",
		"requestDate"=>date("Y-m-d H:i:s"),
		"data"=>null
	]);
}

$_tp_conf_money=$base->doSql('select * from tp_conf_money');

$money=0;
for ($i=0; $i < count($_tp_conf_money); $i++) { 
	if ($_tp_conf_money[$i]["key"]=="chwzhong"  ) {
		$chwzhong=[];
		$chwzhong=explode("公斤",$post["chwzhong"]);
		if ($_tp_conf_money[$i]["value"] <= $chwzhong[0]) {
			$money=$money+$_tp_conf_money[$i]["add_money"];
		}
	}
	if ($_tp_conf_money[$i]["key"]=="chwshuli"  ) {

		if ($_tp_conf_money[$i]["value"] <= $post["chwshuli"]) {
			$money=$money+$_tp_conf_money[$i]["add_money"];
		}
	}
	if ($_tp_conf_money[$i]["key"]=="chwlong"  ) {
	
		if ($_tp_conf_money[$i]["value"] == $post["chwlong"]) {
			$money=$money+$_tp_conf_money[$i]["add_money"];
		}
	}
	if ($_tp_conf_money[$i]["key"]=="chwyunsfun"  ) {
	
		if ($_tp_conf_money[$i]["value"] == $post["chwyunsfun"]) {
			$money=$money+$_tp_conf_money[$i]["add_money"];
		}
	}
	if ($_tp_conf_money[$i]["key"]=="chwyim"  ) {
	
		if ($_tp_conf_money[$i]["value"] == $post["chwyim"]) {
			$money=$money+$_tp_conf_money[$i]["add_money"];
		}
	}
	if ($_tp_conf_money[$i]["key"]=="chwshangmjc1"  ) {
	
		if ($_tp_conf_money[$i]["value"] == $post["chwshangmjc1"]) {
			$money=$money+$_tp_conf_money[$i]["add_money"];
		}
	}
	if ($_tp_conf_money[$i]["key"]=="chwshangmsc1"  ) {
	
		if ($_tp_conf_money[$i]["value"] == $post["chwshangmsc1"]) {
			$money=$money+$_tp_conf_money[$i]["add_money"];
		}
	}
	if ($_tp_conf_money[$i]["key"]=="chwlzxgoods1"  ) {
	
		if ($_tp_conf_money[$i]["value"] == $post["chwlzxgoods1"]) {
			$money=$money+$_tp_conf_money[$i]["add_money"];
		}
	}
	$pycity=[];
	$___pycity__=[];
	if ($_tp_conf_money[$i]["key"]=="pycity"  ) {
		$pycity=explode(",", $_tp_conf_money[$i]["value"] );
		$___pycity__=explode(",", $post["chwshoujiguo"] )[0];
		for ($a=0; $a < count($pycity); $a++) { 
			if ($pycity[$a]==$___pycity__) {
				$money=$money+$_tp_conf_money[$i]["add_money"];
				break;
			}
		}
	}
	if ($_tp_conf_money[$i]["key"]=="mode"  ) {
	
		if ($_tp_conf_money[$i]["value"] == $post["mode"]) {
			$money=$money+$_tp_conf_money[$i]["add_money"];
		}
	}

}


$bfmoney=0;
$tp_bao=$base->doSql('select * from tp_bao');
for ($i=0; $i < count($tp_bao); $i++) { 
	//1 <= 400 && 500 >= 400 
	if ( $tp_bao[$i]["jishud"] <= $money && $tp_bao[$i]["jishug"] >= $money ) {
		$bfmoney=$tp_bao[$i]["id"];
		break;
	}
}



$r=$base->doSql('insert into tp_order set uid="'.$user[0]["id"].'",mode="'.$post["mode"].'",order_on="'.$orderon.'",chwtype="'.$post["chwtype"].'",chwpingzhong="'.$post["chwpingzhong"].'",chwzhong="'.$post["chwzhong"].'",chwshuli="'.$post["chwshuli"].'",chwlong="'.$post["chwlong"].'",chwxingb="'.$post["chwxingb"].'",chwcolor="'.$post["chwcolor"].'",chwic="'.$post["chwic"].'",chwchush="'.$post["chwchush"].'",chwyim="'.$post["chwyim"].'",chwjicity="'.$post["chwjicity"].'",chwjinickname="'.$post["chwjinickname"].'",chwjiphone="'.$post["chwjiphone"].'",chwshoujiguo="'.$post["chwshoujiguo"].'",chwshoujinickname="'.$post["chwshoujinickname"].'",chwshoujiphone="'.$post["chwshoujiphone"].'",chwyunsfun="'.$post["chwyunsfun"].'",chwshangmjc="'.$post["chwshangmjc"].'",chwshangmsc="'.$post["chwshangmsc"].'",chwlzxgoods="'.$post["chwlzxgoods"].'",atime="'.time().'",money="'.$money.'",bfmoney="'.$bfmoney.'"');


$insid=$base->doSql('select LAST_INSERT_ID()');

if ($r) {
	return json([
		"code"=>200,
		"msg"=>"Successful",
		"requestDate"=>date("Y-m-d H:i:s"),
		"data"=>$insid[0]["LAST_INSERT_ID()"]
	]);
}else{
	return json([
		"code"=>400,
		"msg"=>"System Error",
		"requestDate"=>date("Y-m-d H:i:s"),
		"data"=>null
	]);
}


